package com.longbig.multifunction.helper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @author Administrator
 * @date 2024/2/28 10:24
 * @description
 */
public class Md5Helper {

    /**
     * 对字符串进行MD5加密并返回16进制字符串
     *
     * @param input 需要加密的字符串
     * @return MD5加密后的16进制字符串
     */
    public static String encodeMd5(String input) {
        try {
            // 获取MD5的MessageDigest实例
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 计算MD5哈希值
            byte[] digest = md.digest(input.getBytes());

            // 将字节数组转换为16进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }

            return sb.toString();
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("MD5算法未找到", e);
        }
    }
}
